/** 
 * @file Load the DataTables plugin on the HTML tables marked with the class bxtr-autoload-datatables
 */
/*
 * This file is part of the BooXtore Student project.
 *
 * (c) Francis Backeland f.backeland@gmail.com
 * (c) Laurine Chaplot chaplot.laurine@orange.fr
 * (c) Julien Dutot julien.dutot@gmail.com
 * (c) Romain Guillaume romain-guillaume@orange.fr
 * (c) Guillaume Paringaux g.paringaux@gmail.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code
 */
jQuery(document).ready(function() {

    $('.bxtr-autoload-datatables-popup').click(function(event) {
        event.preventDefault();

        // Fetch select hidden field.
        // This field should contain the selected row id, it is used by the server.
        var selectField = $('input#' + $(this).closest('form').find('table.bxtr-autoload-datatables').data('selection-field-id')); 
        var selectedId = selectField.prop('value');
        var placeholderUrl = $(this).data('popup-url');
        var finalUrl = placeholderUrl.replace(/__ID__/, selectedId);
        var popup = jQuery('<div></div>');
        var popupWrapper = jQuery('<div class="popup-wrapper"></div>').append(jQuery('<div></div>').append(popup));

        popup.load(finalUrl, function()
        {
            jQuery(document.body).append(popupWrapper);
            var removeButton = popup.prepend(jQuery('<span class="glyphicon glyphicon-remove"></span>').click(function() {
                popupWrapper.remove();
            }));
            autoLoadTables('table.bxtr-autoload-datatables', popup);

        });
//        console.log(popup);
        //document.body.appendChild(popup);
//        autoLoadTables('table.bxtr-autoload-datatables', jQuery(popup));
    });
});
